﻿var confirmDeleteSelectMsg = "确认删除选择项？";
var template = '<span style="color:{0};">{1}</span>';
var change = function (value) {
    return String.format(template, (value > 0) ? "green" : "red", value);
};

var pctChange = function (value) {
    return String.format(template, (value > 0) ? "green" : "red", value + "%");
};

function GridPanelRowCommand(grid, command, record, editfrom, win, render) {
    var tab, id;
    switch (command) {
        case "Edit":
            var uf = editfrom.getForm();
            uf.loadRecord(record);
            editfrom.record = record;
            try {
                if (editfrom.id == "EditMemuFrom") {
                    window.desEditor.setContent(record.data.Description);
                    if (window.memoEditor != undefined) {
                        window.memoEditor.setContent(record.data.Memo);
                    }
                }
                else if (editfrom.id == "EditArticleFrom") {
                    window.desEditor.setContent(record.data.Content);
                }
            }
            catch (e) {
                //Ext.Msg.alert("提示", e.toString());
            }
            win.show(render);
            break;
        case "Delete":
            var msg = "确认删除   \"" + record.data.Name + "\" ？"
            Ext.Msg.confirm("确认", msg,
                    function (btn) {
                        if (btn == "yes") {

                            switch (grid.id) {
                                case "UserGridPanel":
                                    Ext.net.DirectMethods.DeleteUser(record.data, {
                                        success: function (result) {
                                            if (result == true) {
                                                grid.store.remove(record);
                                                grid.store.commitChanges();
                                            }
                                        },
                                        failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                                    });
                                    break;
                                case "MemuGridPanel":
                                    Ext.net.DirectMethods.DeleteMemu(record.data, {
                                        success: function (result) {
                                            if (result == true) {
                                                grid.store.remove(record);
                                                grid.store.commitChanges();
                                            }
                                        },
                                        failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                                    });
                                    break;
                                case "PartGridPanel":
                                    Ext.net.DirectMethods.DeletePart(record.data, {
                                        success: function (result) {
                                            if (result == true) {
                                                grid.store.remove(record);
                                                grid.store.commitChanges();
                                            }
                                        },
                                        failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                                    });
                                    break;
                                case "ArticleGridPanel":
                                    Ext.net.DirectMethods.DeleteArticle(record.data, {
                                        success: function (result) {
                                            if (result == true) {
                                                grid.store.remove(record);
                                                grid.store.commitChanges();
                                            }
                                        },
                                        failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                                    });
                                    break;
                                case "gpServerConfig":
                                    Ext.net.DirectMethods.DeleteServerConfig(record.data, {
                                        success: function (result) {
                                            if (result == true) {
                                                grid.store.remove(record);
                                                grid.store.commitChanges();
                                            }
                                        },
                                        failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                                    });
                                    break;
                            }
                        }
                    });
            break;
        case "ShowChild":

            id = record.data.Name + "K" + record.data.ID;
            tab = window.tpContent.getComponent(id);

            if (!tab) {
                tab = window.tpContent.add({
                    id: id,
                    title: record.data.Name,
                    closable: true,
                    autoLoad: {
                        showMask: true,
                        url: RootUrl() + "Main/Admin/ChildMemu.aspx?mPartId=" + record.data.ID,
                        mode: "iframe",
                        maskMsg: "正在加载…"
                    }
                });
            }
            window.tpContent.setActiveTab(tab);
            break;
        case "ShowArticle":
            id = record.data.Name +"K" + record.data.ID;
            var tpContent = window.parent.tpContent;

            tab = tpContent.getComponent(id);

            if (!tab) {
                tab = tpContent.add({
                    id: id,
                    title: record.data.Name,
                    closable: true,
                    autoLoad: {
                        showMask: true,
                        url: RootUrl() + "Main/Admin/ArticleList.aspx?partId=" + record.data.ID,
                        mode: "iframe",
                        maskMsg: "正在加载…"
                    }
                });
            }
            tpContent.setActiveTab(tab);
            break;

        case "ShowServer":
            id = record.data.Name +"K"+ record.data.ID;
            var tpContent = window.parent.tpContent;

            tab = tpContent.getComponent(id);

            if (!tab) {
                tab = tpContent.add({
                    id: id,
                    title: record.data.Name,
                    closable: true,
                    autoLoad: {
                        showMask: true,
                        url: RootUrl() + "Main/Admin/ServerConfigList.aspx?mPartId=" + record.data.ID,
                        mode: "iframe",
                        maskMsg: "正在加载…"
                    }
                });
            }
            tpContent.setActiveTab(tab); break;
    }
}
function DeleteSeleteItems(grid) {
    var msg = "确认删除所选择的项？"
    Ext.Msg.confirm("确认", msg,
                    function (btn) {
                        if (btn == "yes") {
                            switch (grid) {
                                case UserGridPanel:
                                    Ext.Msg.alert("true", UserGridPanel.id);
                                    break;
                                case MemuGridPanel:
                                    Ext.Msg.alert("true", MemuGridPanel.id);
                                    break;
                                case ArticleGridPanel:
                                    Ext.Msg.alert("true", ArticleGridPanel.id);
                                    break;
                            }
                        }
                    });
}


function TreeNode_Click(node) {
    var id;
    switch (node.id) {
        case "1":
            id = window.UserGridPanel.id;
            break;
        case "2":
            id = MemuGridPanel.id;
            break;
        case "3":
            id = PartGridPanel.id;
            break;
        case "4":
            id = "网站站点配置";
            break;
    }

    var tpContent = window.tpContent;

    var tab = tpContent.getComponent(id);

    if (!tab) {
        var mk = new Ext.LoadMask(Ext.get('tpContent'), {
            msg: '正在加载数据，请稍候……',
            //完成后移除  
            removeMask: true
        });
        mk.show(); //显示  

        switch (node.id) {
            case "1":
                tab = tpContent.add(window.UserGridPanel);
                window.UserGridPanel.store.reload({
                    callback: function () {
                        mk.hide();
                    }
                });
                break;
            case "2":
                tab = tpContent.add(MemuGridPanel);
                window.MemuGridPanel.store.reload({
                    callback: function () {
                        mk.hide();
                    }
                });
                break;
            case "3":
                tab = tpContent.add(PartGridPanel);
                window.PartGridPanel.store.reload({
                    callback: function () {
                        mk.hide();
                    }
                });
                break;
            case "4":
                var tpContent = window.parent.tpContent;
                mk.hide();
                tab = tpContent.getComponent(id);

                if (!tab) {
                    tab = tpContent.add({
                        id: id,
                        icon: "cog",
                        title: id,
                        closable: false,
                        autoLoad: {
                            showMask: true,
                            url: RootUrl() + "Main/Admin/WebSiteConfig.aspx",
                            mode: "iframe",
                            maskMsg: "正在加载…"
                        }
                    });
                }
                tpContent.setActiveTab(tab); break;
                break;
        }

    }
    tpContent.setActiveTab(tab);

    //    Ext.net.DirectMethods.TreeNode_Click(item,
    //            {
    //                success: function (result) {
    //                    mk.hide();
    //                },
    //                failure: function () { mk.hide(); }
    //            });
}

function SaveData(identity, form, isadd, win, grid) {
    if (!form.getForm().isValid()) {
        Ext.net.Notification.show({
            iconCls: "icon-exclamation",
            html: "Form is invalid",
            title: "Error"
        });

        return false;
    }
    var myNewRecord, record;
    if (isadd == true) {
        var data = form.getForm().getFieldValues(false, "dataIndex");
        var TopicRecord = Ext.data.Record.create(form.record);
        myNewRecord = new TopicRecord(data);
        myNewRecord.data.ID = 0;
        record = myNewRecord;
    } else {
        record = form.record;
        if (record.data.ID == 0) {
            isadd = true;
        }
    }

    if (form.id == "EditMemuFrom") {
        record.data.Description = window.desEditor.getContent();
        if (window.memoEditor != undefined) {
            record.data.Memo = window.memoEditor.getContent();
        }
    }
    else if (form.id == "EditArticleFrom") {
        record.data.Content = window.desEditor.getContent();
    }

    switch (identity) {
        case 1:
            Ext.net.DirectMethods.SaveUser(record.data, {
                success: function (result) {
                    if (isadd == true) {
                        form.getForm().reset();
                        record.data.ID = result;
                        grid.store.add(record);
                        record.commit();
                        grid.store.commitChanges();
                    }
                    else {
                        win.hide();
                    }
                    record.commit();
                    record.store.reload();
                },
                failure: function (msg) { Ext.Msg.alert("提示", "操作失败！\n 详细：" + msg); }
            });
            break;
        case 2:
            Ext.net.DirectMethods.SaveMemu(record.data,
                {
                    success: function (result) {
                        if (isadd == true) {
                            form.getForm().reset();
                            record.data.ID = result;
                            grid.store.add(record);
                            record.commit();
                            grid.store.commitChanges();
                        }
                        else {
                            win.hide();
                        }
                        record.commit();
                        record.store.reload();
                    },
                    failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                });
            break;
        case 3:
            Ext.net.DirectMethods.SaveArticle(record.data,
                {
                    success: function (result) {
                        if (isadd == true) {
                            form.getForm().reset();
                            record.data.ID = result;
                            grid.store.add(record);
                            record.commit();
                            grid.store.commitChanges();
                        }
                        else {
                            win.hide();
                        }
                        record.commit();
                        record.store.reload();
                    },
                    failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                });
            break;
        case 4:
            Ext.net.DirectMethods.SaveServerConfig(record.data,
                {
                    success: function (result) {
                        if (isadd == true) {
                            form.getForm().reset();
                            record.data.ID = result;
                            grid.store.add(record);
                            record.commit();
                            grid.store.commitChanges();
                        }
                        else {
                            win.hide();
                        }
                        record.commit();
                        record.store.reload();
                    },
                    failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                });
            break;
        case 5:
            Ext.net.DirectMethods.SavePart(record.data,
                {
                    success: function (result) {
                        if (isadd == true) {
                            form.getForm().reset();
                            record.data.ID = result;
                            grid.store.add(record);
                            record.commit();
                            grid.store.commitChanges();
                        }
                        else {
                            win.hide();
                        }
                        record.commit();
                        record.store.reload();
                    },
                    failure: function (msg) { Ext.Msg.alert("提示", "操作失败！<br /> 详细：" + msg); }
                });
            break;
    }
}

var RootUrl = function () {
    return window.location.protocol + "//" + window.location.host + "/";
};


function NewData(editfrom, win, grid) {
    var uf = editfrom.getForm();

    var data = editfrom.getForm().getFieldValues(false, "dataIndex");
    var TopicRecord = Ext.data.Record.create(grid.store.fields.items);
    var myNewRecord = new TopicRecord(data);
    myNewRecord.data.ID = 0;


    uf.loadRecord(myNewRecord);
    editfrom.record = myNewRecord;
    win.show(grid);
}